package com.gpframework.module.ctn.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.ctn.domain.*;
import com.gpframework.module.ctn.domain.req.GpNigeriaWarehouseReleaseAdd;
import com.gpframework.module.ctn.domain.vo.GpNigeriaWarehouseReleaseExcel;
import com.gpframework.module.ctn.domain.vo.GpNigeriaWarehouseReleaseVo;
import com.gpframework.module.ctn.mapper.GpNigeriaWarehouseReleaseMapper;
import com.gpframework.module.ctn.service.IGpNigeriaWarehouseReleaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @ClassName GpNigeriaWarehouseRelease
 * @Author gpframework
 * @Date 2024-01-08
 * @Version 1.0.0
 * @Description 尼仓库放货Service业务层处理
 */
@Service
public class GpNigeriaWarehouseReleaseServiceImpl extends ServiceImpl<GpNigeriaWarehouseReleaseMapper, GpNigeriaWarehouseRelease> implements IGpNigeriaWarehouseReleaseService {

    @Autowired
    private GpReceiptGoodsServiceImpl gpReceiptGoodsService;
    @Autowired
    private GpCtnOrderServiceImpl gpCtnOrderService;
    @Autowired
    private GpPackagesInformationServiceImpl gpPackagesInformationService;
    @Autowired
    private GpCtnLoadedServiceImpl gpCtnLoadedService;
    @Autowired
    private GpSackInformationServiceImpl gpSackInformationService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [lqw]
     * @Return List<GpNigeriaWarehouseReleaseVo>
     */
    @Override
    public List<GpNigeriaWarehouseReleaseVo> listVo(QueryWrapper<GpNigeriaWarehouseRelease> lqw) {
        return this.baseMapper.listVo(lqw);
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2024-01-08
     * @Param [id]
     * @Return GpNigeriaWarehouseReleaseVo
     */
    @Override
    public GpNigeriaWarehouseReleaseVo getVoById(Long id) {
        return this.baseMapper.getVo(id);
    }

    @Override
    public List<GpNigeriaWarehouseReleaseExcel> gpNigeriaWarehouseReleaseImport(List<GpNigeriaWarehouseReleaseExcel> excelList, Long brandId, Long storeId) {
        //导入失败的列表
        List<GpNigeriaWarehouseReleaseExcel> failureList = new ArrayList<>();
        //尼仓库放货
        GpNigeriaWarehouseRelease gpNigeriaWarehouseRelease;

        for (int i = 0; i < excelList.size(); i++) {
            GpNigeriaWarehouseReleaseExcel excel = excelList.get(i);
            if (StringUtils.isNull(excel)) {
                excel = new GpNigeriaWarehouseReleaseExcel();
                excel.setSubscript(i+1);
                excel.setMsg("读取数据失败");
                failureList.add(excel);
                continue;
            }
            gpNigeriaWarehouseRelease = Convert.convert(new TypeReference<GpNigeriaWarehouseRelease>() {}, excel);
            this.save(gpNigeriaWarehouseRelease);
        }
        return failureList;
    }


    @Override
    public boolean addNnigeriaWareHouserelease(GpNigeriaWarehouseReleaseAdd gpNigeriaWarehouseReleaseAdd) {
        GpNigeriaWarehouseRelease gpNigeriaWarehouseRelease = Convert.convert(new TypeReference<GpNigeriaWarehouseRelease>() {
        }, gpNigeriaWarehouseReleaseAdd);

        GpCtnOrder gpCtnOrder = gpCtnOrderService.getById(gpNigeriaWarehouseReleaseAdd.getCtnOrderId());
        GpReceiptGoods gpReceiptGoods = gpReceiptGoodsService.getById(gpNigeriaWarehouseReleaseAdd.getReceiptId());
        GpPackagesInformation gpPackagesInformation = gpPackagesInformationService.getById(gpNigeriaWarehouseReleaseAdd.getPackId());
        if (gpCtnOrder!=null){
            //从定货柜拿
            gpNigeriaWarehouseRelease.setAgentCtnNo(gpCtnOrder.getAgentCtnNo());
        }
        if (gpReceiptGoods!=null){
            //从订单管理拿
            gpNigeriaWarehouseRelease.setCustomer(gpReceiptGoods.getCustomer());
            gpNigeriaWarehouseRelease.setCustomerTel(gpReceiptGoods.getCustomerTel());
            gpNigeriaWarehouseRelease.setSupplier(gpReceiptGoods.getSupplier());
            gpNigeriaWarehouseRelease.setSupplierTel(gpReceiptGoods.getSupplierTel());
            gpNigeriaWarehouseRelease.setReceiptNo(gpReceiptGoods.getReceiptNo());
            gpNigeriaWarehouseRelease.setGoodsDescription(gpReceiptGoods.getGoodsDescription());
        }
        if (gpPackagesInformation!=null){
            //从打包信息里面拿
            gpNigeriaWarehouseRelease.setPackNo(gpPackagesInformation.getPackNo());
            gpNigeriaWarehouseRelease.setWeight(gpPackagesInformation.getPackWeight());
            gpNigeriaWarehouseRelease.setUnit(gpPackagesInformation.getUnit());
            gpNigeriaWarehouseRelease.setCbm(gpPackagesInformation.getCbm());
            gpNigeriaWarehouseRelease.setHoldOrNot(gpPackagesInformation.getHoldOrNot());
        }


        return this.save(gpNigeriaWarehouseRelease);
    }

    @Override
    public boolean addBatch(GpNigeriaWarehouseReleaseAdd gpNigeriaWarehouseReleaseAdd) {
        List<GpNigeriaWarehouseRelease> gpNigeriaWarehouseReleaseList = gpNigeriaWarehouseReleaseAdd.getGpNigeriaWarehouseReleaseList();
        if (CollectionUtils.isNotEmpty(gpNigeriaWarehouseReleaseList)){
            for (int i = 0; i < gpNigeriaWarehouseReleaseList.size(); i++) {
                GpNigeriaWarehouseRelease gpNigeriaWarehouseRelease = gpNigeriaWarehouseReleaseList.get(i);
                if (CollectionUtils.isNotEmpty(gpNigeriaWarehouseRelease.getPackagesInformationList())){
                    //尼日仓库打包放货
                    this.nigeriaWarehouseReleasePack(gpNigeriaWarehouseRelease);
                }
                if (CollectionUtils.isNotEmpty(gpNigeriaWarehouseRelease.getSackInformationList())){
                    //尼日仓库散货放货
                    this.nigeriaWarehouseReleaseSack(gpNigeriaWarehouseRelease);
                }

            }
        }
        return true;
    }

    private void nigeriaWarehouseReleaseSack(GpNigeriaWarehouseRelease gpNigeriaWarehouseRelease) {
        for (GpSackInformation gpSackInformation : gpNigeriaWarehouseRelease.getSackInformationList()) {
            GpNigeriaWarehouseRelease nigeriaWarehouseRelease = Convert.convert(new TypeReference<GpNigeriaWarehouseRelease>() {}, gpSackInformation);
            nigeriaWarehouseRelease.setNigeriaReleaseDate(gpSackInformation.getNigeriaReleaseDate());

            // 判断新增还是修改
            if (gpSackInformation.getNigeriaWarehouseReleaseId()!=null){
                nigeriaWarehouseRelease.setId(gpSackInformation.getNigeriaWarehouseReleaseId());
            }else {
                nigeriaWarehouseRelease.setId(null);
            }
            this.saveOrUpdate(nigeriaWarehouseRelease);

            //更新打包信息 尼仓库放货日期
            GpSackInformation sackInformation = gpSackInformationService.getById(gpSackInformation.getId());
            sackInformation.setNigeriaReleaseDate(gpSackInformation.getNigeriaReleaseDate());
            sackInformation.setNigeriaWarehouseReleaseId(nigeriaWarehouseRelease.getId());
            gpSackInformationService.updateById(sackInformation);
        }
    }

    private void nigeriaWarehouseReleasePack(GpNigeriaWarehouseRelease gpNigeriaWarehouseRelease) {
        for (GpPackagesInformation gpPackagesInformation : gpNigeriaWarehouseRelease.getPackagesInformationList()) {
            GpNigeriaWarehouseRelease nigeriaWarehouseRelease = Convert.convert(new TypeReference<GpNigeriaWarehouseRelease>() {}, gpPackagesInformation);
            nigeriaWarehouseRelease.setNigeriaReleaseDate(gpPackagesInformation.getNigeriaReleaseDate());

            // 判断新增还是修改
            if (gpPackagesInformation.getNigeriaWarehouseReleaseId()!=null){
                nigeriaWarehouseRelease.setId(gpPackagesInformation.getNigeriaWarehouseReleaseId());
            }else {
                nigeriaWarehouseRelease.setId(null);
            }
            this.saveOrUpdate(nigeriaWarehouseRelease);

            //更新打包信息 尼仓库放货日期
            GpPackagesInformation packagesInformation = gpPackagesInformationService.getById(gpPackagesInformation.getId());
            packagesInformation.setNigeriaReleaseDate(gpPackagesInformation.getNigeriaReleaseDate());
            packagesInformation.setNigeriaWarehouseReleaseId(nigeriaWarehouseRelease.getId());
            gpPackagesInformationService.updateById(packagesInformation);

        }
    }


}
